package com.ycsj.goldmedalnewconcept.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.NetConstant;
import com.ycsj.goldmedalnewconcept.utils.MyWebViewClient;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

import java.io.IOException;

public class MineShoucangTestFragment extends Fragment {
	View rootView;
	private WebView webView;
	private WebView webView2;
	private FrameLayout video_fullView;
	private View xCustomView;
	private CustomViewCallback xCustomViewCallback;
	private ProgressBar mprogressBar;
	@SuppressLint("JavascriptInterface")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView=inflater.inflate(R.layout.fragment_sc_left, null);
		video_fullView = (FrameLayout) rootView.findViewById(R.id.video_fullView_sc);
		mprogressBar = (ProgressBar) rootView.findViewById(R.id.mProgress);

		webView=(WebView) rootView.findViewById(R.id.webView_sc);
		webView2=(WebView) rootView.findViewById(R.id.webView_sc2);
		int lESSON_ID = getActivity().getIntent().getIntExtra("lESSON_ID", 0);
		// 让webview去加载网页
		
		
		//旧版传值方法
//		webView.loadUrl(NetConstant.H5+"App_Class/H5/ReqParaFromApp.aspx?account="
//		+SPUtils.getString(getActivity(),ConfigConstant.USERNAME)+"&type=&lessonid=");
		
		
		
		
		
		
		
		// 没有显示出点击展开全文的原因是：webview默认没有开启javascript功能
		// 因为webview上有很多设置，所以webview把设置都封装到WebSettings类中了
		WebSettings settings = webView.getSettings();
		WebSettings settings2 = webView2.getSettings();
		settings.setJavaScriptEnabled(true);
		settings2.setJavaScriptEnabled(true);
		// webview可以设置监听得到各种事件
		// 1 WebViewClient 比较简单的监听
		// webView.setWebViewClient(client);
		// 2 比较复杂、高级的监听
//		webView.setWebChromeClient(chromeClient);
//		webView2.setWebChromeClient(chromeClient);
		// 设置文字大小
		// 从sp中读取String -->TextSize
		
		
		webView2.getSettings().setJavaScriptEnabled(true);
		webView2.getSettings().setBuiltInZoomControls(true);
		webView2.getSettings().setUseWideViewPort(true);
		webView2.getSettings().setRenderPriority(RenderPriority.HIGH);
		// mWebView.getSettings().setBlockNetworkImage(true);
		webView2.loadData("", "text/html", null);
		
		
		
		
		//旧版传值
//		webView2.loadUrl("javascript:alert(injectedObject.toString())");
		
		
		
		
		
		webView2.setWebViewClient(new MyWebViewClient(mprogressBar));
		webView2.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub            
				// MainActivity.this.setProgress(newProgress * 100);
				mprogressBar.setProgress(newProgress);
			}
		});
		
		webView.setVisibility(View.GONE);
//		webView2.loadUrl(NetConstant.H5+"html5/Page_keepList.html");
		webView2.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
//		webView2.loadUrl("http://m.moregolden.com:36356/jpjNewPage/collectPage.html");
//		webView2.loadUrl(NetConstant.H5+"html5/AndroidPage/Android_page_keepList.html");
		webView2.loadUrl("https://moregolden.com:25697/collectPage.html");
		
		
		
		
		return rootView;
	}
	
	private WebChromeClient chromeClient = new WebChromeClient() {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress == 100) {
			}
		}

	};

	private WebViewClient client = new WebViewClient() {
		// 当页面加载完成之后会调用此方法
		@Override
		public void onPageFinished(WebView view, String url) {
			
		}

	};
	
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		webView.clearCache(true);
		webView.clearHistory();
		webView2.clearCache(true);
		webView2.clearHistory();
		
		webView.loadUrl("about:blank");
		webView2.loadUrl("about:blank");
		
		
	}
	
	
	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		 if (mediaPlayer != null) {   
	            mediaPlayer.stop();  
	            mediaPlayer.release();   
	            mediaPlayer = null;   
	        }  
	}
	private MediaPlayer mediaPlayer;
	public class WebAppInterface {
	    Context mContext;
		

	    /** Instantiate the interface and set the context */
	    WebAppInterface(Context c) {
	        mContext = c;
	    }

	    /** Show a toast from the web page */
	    @JavascriptInterface
	    public void showToast(String toast) {

	    	mediaPlayer = new MediaPlayer();
	    	String uri;
//	    	Toast.makeText(mContext, toast, 0).show();
	    	if("http".equals(toast.substring(0, 4))){
	    		 uri=toast;
	    	}else{
	    		 uri=NetConstant.H5+"html5/"+toast;
	    	}
	    	
	    	
	    	mediaPlayer.reset();  
            try {
				mediaPlayer.setDataSource(uri);
				mediaPlayer.prepare();
				mediaPlayer.start(); 
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
            //prepare之后自动播放  
	    
	    }
	    
	    	//传递参数
	  		@JavascriptInterface
	  		public String getParam() {
	  			
	  			String param =SPUtil.getString(mContext, ConfigConstant.USERNAME,"");
	  					
	  			Log.e("test", "param="+param);
	  			
	  			return param;
	  		}
	    
	}
}
